package com.bitjiuyeke;

import utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo05_Update {
    public static void main(String[] args) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        Scanner scanner = new Scanner(System.in);
        try {
            // 1. 获取数据库连接
            connection = DBUtil.getConnection();
            // 2. 定义SQL语句, 根据Id修改学号
            String sql = "update student set sno = ? where id = ?";
            // 3. 定义SQL的预处理对象
            statement = connection.prepareStatement(sql);
            // 4. 用真实的数据替换占位符

            System.out.println("请输入要修改的记录Id-->");
            long inId = scanner.nextLong();
            System.out.println("请输入要修改的学号-->");
            String inSno = scanner.next();


            statement.setString(1, inSno);
            statement.setLong(2, inId);
            // 5. 执行SQL
            int row = statement.executeUpdate();
            // 6. 获取执行结果
            if (row >0) {
                System.out.println("修改成功");
            } else {
                System.out.println("修改失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            // 释放资源
            DBUtil.close(null, statement, connection);
            scanner.close();
        }

    }
}